/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import com.eelpo.common.util.BlankUtils;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * <pre>
 * 角色实体类，用于映射T_ROLE表(Persist Object)
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Entity
@Cacheable
@Table(name = "T_ROLE")
public class TRole implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -3675292213943458802L;

	/**
	 * 角色Id
	 */
	@NotEmpty
	private String id;

	/**
	 * 角色名称
	 */
	@NotEmpty
	private String name;

	/**
	 * 角色状态
	 */
	@NotNull
	private Byte status;

	/**
	 * 添加时间
	 */
	@NotNull
	private Date addTime;

	/**
	 * 角色描述信息
	 */
	private String description;

	/**
	 * 角色下的所有用户
	 */
	private Set<TUser> users = new HashSet<TUser>();

	/**
	 * 角色拥有的权限
	 */
	private Set<TPermission> permissions = new HashSet<TPermission>();

	public TRole() {

	}

	public TRole(String id) {
		this.id = id;
	}

	@Id
	@Column(length = 36)
	@GenericGenerator(name = "uuid", strategy = "uuid2")
	@GeneratedValue(generator = "uuid")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(nullable = false, length = 64, unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(nullable = false)
	public Byte getStatus() {
		return status;
	}

	public void setStatus(Byte status) {
		this.status = status;
	}

	@Column(nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	public Date getAddTime() {
		return addTime;
	}

	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}

	@Column(length = 512)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "T_USER_ROLE", joinColumns = { @JoinColumn(name = "roleId", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "userId", referencedColumnName = "id") })
	public Set<TUser> getUsers() {
		return users;
	}

	public void setUsers(Set<TUser> users) {
		this.users = users;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "T_ROLE_PERMISSION", joinColumns = { @JoinColumn(name = "roleId", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "permissionId", referencedColumnName = "id") })
	public Set<TPermission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<TPermission> permissions) {
		this.permissions = permissions;
	}

	@Override
	public boolean equals(Object obj) {
		if (BlankUtils.isBlank(obj) || !(obj instanceof TRole))
			return false;

		if (this == obj)
			return true;

		TRole role = (TRole) obj;

		if (BlankUtils.isBlank(this.id) || BlankUtils.isBlank(role.getId())) {
			return false;
		}

		return this.id.equals(role.getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public String toString() {
		return "TRole[name=" + name + "]";
	}
}
